﻿//Load script common
function lzLoading() {
    // load src cua cac img cho lazy load thuan tien cho seo
    $("img.lzLoading").each(function () {
        $(this).attr("data-original", $(this).attr("src"));
        $(this).attr("src", "/images/icons/blank.gif");
    });
    //lazy load
    $('img.lzLoading').lazyload({
        effect: "fadeIn"
        //threshold: 200
    });
}

$(function () {
    //header 
    HoverCartHeader();
    
    //show Navibar
    ShowNavibar();

    // dinh Danh muc san pham len top khi scroll xuonh
    scrollHome();


    // SEARCH AUTO COMPLETE --------------------------------------------------------------
    $('.input_search').autocomplete({
        serviceUrl: '/Home/SearchAutoComplete',
        minChars: 1,
        delimiter: /(,|;)\s*/, // regex or character
        zIndex: 10000,
        noCache: false
    });


    //--------- Popup Advertise -----------------------
    var show = $('.showPopup').val();
    if (show == "1") {
        if (typeof $.cookie('advertise') == 'undefined') {
            $('.popup-wap').show();
        } else {
            $('.popup-wap').hide();
        }
    }
    else {
        $('.popup-wap').hide();
    }
    $('.popup-close, a.pn-closepopup').click(function () {
        $('.popup-wap').hide();
        $.cookie('advertise', 1);
    });
$('#footer').append('<div class="scroll-top"></div>');
    // Scroll Top
    $(window).scroll(function () {
        if ($(window).scrollTop() != 0) {
            $('.scroll-top').fadeIn();
        } else {
            $('.scroll-top').fadeOut();
        }
    });

    $('.scroll-top').live('click', function () {
        $('body,html').animate({ scrollTop: 0 }, 500);
    });

});

function ShowTabNewsFooter() {
    $('.tabFooter li').click(function () {
        $('.tabFooter li').removeClass('tab-f-ac');
        $(this).addClass('tab-f-ac');
        $('ul.tab-f-sub').hide();
        $(this).find('ul.tab-f-sub').show();
    });
}
function toSplitNumberByComma(ctr) {
    var val = ctr.val();
    if (val != null && val != '') ctr.val(val.split(',')[0]);
    ctr.mask('000.000.000.000.000', { reverse: true });
}
function scrollHome() {
    //copy danh muc con vao header
    $('#mnu-plc-header .mnu-list').remove();
    $('#mnu-plc-header').append($('.mnu-box .mnu-list').clone().stop(true, true));
    var cmdMenuBox = $('.mnu-box #mnu-plc-header');
    if (cmdMenuBox != undefined) {
        var khoangCachTopMenu = cmdMenuBox.parent().offset().top;
        var khoangCachTopSubMenu = cmdMenuBox.parent().offset().top;
        // su kien khi scrol
        $(window).scroll(function () {
            //var khoangCachDuoi = cmdMenuBox.height() + khoangCachTopMenu;
            //alert(khoangCachDuoi);
            if ($(window).scrollTop() != 0) {
                if ($(window).scrollTop() >= khoangCachTopMenu) {
                    cmdMenuBox.css({ 'top': '-3px', 'position': 'fixed' });
                    cmdMenuBox.parent().css({ 'padding-top': '36px' });
                } else {
                    cmdMenuBox.removeAttr('style');
                    cmdMenuBox.parent().removeAttr('style');
                }

                if ($(window).scrollTop() >= khoangCachTopSubMenu) {
                    $('#mnu-plc-header').addClass('mnu-plc-header-hover');
                } else {
                    $('#mnu-plc-header').removeClass('mnu-plc-header-hover');
                }

            } else {
                cmdMenuBox.removeAttr('style');
                cmdMenuBox.parent().removeAttr('style');
            }

            //var khoangCachDuoi = $('#footer').offset().top;
            //var doCaoBox = $('.productCusInteres').height();
            //if ($(window).scrollTop() != 0) {
            //    //alert(DoCaoBox + '+' + KhoangCachDuoi);
            //    if ($(window).scrollTop() >= khoangCachTop - 40) {
            //        $('.productCusInteres').css({ 'top': '40px', 'position': 'fixed' });
            //        $('.productCusInteres').addClass('borderShadow');
            //        if ($(window).scrollTop() + doCaoBox >= khoangCachDuoi) {
            //            $('.productCusInteres').css({ 'top': (khoangCachDuoi - doCaoBox) + 'px', 'position': 'absolute' });
            //            $('.productCusInteres').removeClass('borderShadow');
            //        }
            //    } else {
            //        $('.productCusInteres').removeAttr('style');
            //        $('.productCusInteres').removeClass('borderShadow');
            //    }

            //} else {
            //    $('.productCusInteres').removeAttr('style');
            //    $('.productCusInteres').removeClass('borderShadow');
            //}
        });
    }
    
}


function HoverCartHeader() {
    $('div.pnCart').hover(function () {
        $('div.BoxCartHover').toggle();
    });
}

function ShowNavibar() {
    $('.NaviHover').hover(function () {
        $(this).parent().toggleClass('navi-item-ac');
        $(this).children('.SubNavi').toggle();
    });
}

function MenuTwoLevelEffect(container) {
    var aTag = container.find('li > a');
    aTag.click(function () {
        var parent = $(this).parent();
        $(parent).toggleClass('bb_cate_ac');
        $(parent).children('.bb_c_l2').slideToggle();
    });
}

function getNumberOfDays(year, month) {
    var isLeap = ((year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0));
    return [31, (isLeap ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
}
function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
    return pattern.test(emailAddress);
};
function formatNumber(str) {
    var amount = new String(str);
    amount = amount.split("").reverse();

    var output = "";
    for (var i = 0; i <= amount.length - 1; i++) {
        output = amount[i] + output;
        if ((i + 1) % 3 == 0 && (amount.length - 1) !== i) output = '.' + output;
    }
    return output;
}

